﻿using System;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Xml;
using System.Configuration;
using System.Web.Security;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string rssURL = "http://news.google.com/news?pz=1&ned=us&hl=en&as_scoring=n&geo=franklin+county%2C+ohio&as_maxm=5&q=foreclosure&as_qdr=m&as_drrb=q&as_mind=26&as_minm=4&as_maxd=26&output=rss";
        //  RssFeed.Text = "<font size=5><b>" + "Franklin County Foreclosure News" + "</b></font><Br />";
        ProcessRSSItem(rssURL);
        getRecentTransfers();
    }

    public void getRecentTransfers()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings.Get("strConn"));
        SqlDataReader rdr = null;

        try
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT street_address, property_id FROM property WHERE property_id IN (SELECT property_id FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY Date DESC) as row FROM Property_Transfer) a WHERE row <= 5)", conn);
            rdr = cmd.ExecuteReader();

            int pid = 0;
            string addr = null;
            while (rdr.Read())
            {
                pid = (int)rdr[1];
                addr = (string)rdr[0];
                Recent.Text += "<a href='http://propertyiq.us/Property_Info/Property_Details.aspx?pid=" + pid + "'>" + addr + "</a><br />";

            }
        }
        finally
        {
            if (rdr != null)
            {
                rdr.Close();
            }
            if (conn != null)
            {
                conn.Close();
            }
        }
    }

    private string parseDate(string date)
    {
        string[] parts = date.Split(' ');
        return parts[2] + ' ' + parts[1] + ", " + parts[3];
    }

    public void ProcessRSSItem(string rssURL)
    {
        WebRequest myRequest = WebRequest.Create(rssURL);
        WebResponse myResponse = myRequest.GetResponse();

        Stream rssStream = myResponse.GetResponseStream();
        XmlDocument rssDoc = new XmlDocument();
        rssDoc.Load(rssStream);

        XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

        string title = "";
        string link = "";
        string pubDate = "";

        for (int i = 0; i < 3; i++)
        {
            XmlNode rssDetail;

            rssDetail = rssItems.Item(i).SelectSingleNode("title");
            if (rssDetail != null)
            {
                title = rssDetail.InnerText;
            }
            else
            {
                title = "";
            }
            rssDetail = rssItems.Item(i).SelectSingleNode("link");
            if (rssDetail != null)
            {
                link = rssDetail.InnerText;
            }
            else
            {
                link = "";
            }
            rssDetail = rssItems.Item(i).SelectSingleNode("pubDate");
            if (rssDetail != null)
            {
                pubDate = rssDetail.InnerText;
            }
            else
            {
                pubDate = "";
            }

            RssFeed.Text += "<b><a href='" + link + "' target='new'>" + title + "</a></b><br/>";
            RssFeed.Text += parseDate(pubDate) + "</p><br/>";
        }
    }


    protected void logout_click(Object sender, EventArgs e)
    {
        Session.Abandon();
        FormsAuthentication.SignOut();
        Response.Redirect("#");



    }
}
